package com.bjsxt.common.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bjsxt.users.pojo.AAdmins;
import com.bjsxt.users.service.IUsersService;

@Controller
public class NoLoginController extends BaseController
{	@Resource
	private IUsersService usersService;
	@RequestMapping("/adminsLogin.htm")
	public String adminsLogin()
	{
		/*
		 * 打开管理员登录界面
		 */
		return "/back/adminsLogin";
	}
	
	/*
	 * 打开管理员登录提交页面
	 */				
	@RequestMapping("/adminsLoginSubmit.htm")
	public String adminsLoginSubmit(HttpServletRequest request,String email,String password,String code)
	{
		HttpSession session=request.getSession();
		/*
		 * 登陆成功,跳转到/index.htm页面
		 * 登陆失败:跳转到/adminsLogin.htm页面
		 * */
		/*
		 * 接收参数
		 * 处理参数
		 * 根据emails查询管理员(数据库)
		 * 做判断
		 * 登陆成功,跳转到/index.htm页面
		 * 登陆失败:跳转到/adminsLogin.htm页面
		 * */
		//从session中取验证码
		String codeSess=session.getAttribute("code")+ "";
		//无论登录成功或者失败,验证码都要重新加载
		session.removeAttribute("code");
		//加个万能验证码
		if("aaaa".equalsIgnoreCase(code)|| (codeSess != null && codeSess.equalsIgnoreCase(code) ) )
		{
			
			/*接收参数 属性驱动 */
			
			//通过email查询数据库		
			Map<String, Object> condMap =new HashMap<String, Object>();
			//将接收到的email放入condMap中
			condMap.put("email", email);
			AAdmins admins=	this.usersService.findOneAdminsService(condMap);
			//判断email是否为空,在判断密码是否正确
			if(admins !=null)
			{
				//判断密码是否正确
				if(this.encryptUtil.checkPassword(password, admins.getPassword()))
				{						
						//判断是否处于禁用状态
						if(admins.getStatus() == 1)
						{    
							//将有用参数放入session中
							session.setAttribute("admins",admins);
							session.setAttribute("lastLoginTime",admins.getLastLoginTime());
							//再把admins的上次登陆时间改成这次的
							admins.setLastLoginTime(new Date());
							//登录成功,跳转登录页面
							return "redirect:/back/admins/index.htm";
						}else
						{
							this.info="此用户已被禁用";
						}
				}else
				{
					this.info="密码不正确";
				}
				request.setAttribute("admins",admins);
			}else
			{
				this.info="用户名错误";
			}
			
		}else
		{
			
			this.info="验证码错误";
		}
		session.setAttribute("info", info);
		return this.adminsLogin();
	}
}
